package com.sas.repositoryImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sas.domain.ArticleField;
import com.sas.repository.ArticleFieldRepository;

@Repository
public class HibernateArticleFieldRepository extends
		HibernateGenericRepository<ArticleField> implements ArticleFieldRepository {
	@Autowired
	public HibernateArticleFieldRepository (SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	@Override
	public List<ArticleField> findRoots() {
		Session session = getSession();
		Query query = session.createQuery("from ArticleField af where af.root = true");
		return query.list();
	}

	@Override
	public ArticleField findByName(String name) {
		Session session = getSession();
		Query query = session.createQuery("from ArticleField af where af.name = ?");
		query.setString(0, name);
		
		return (ArticleField) ((query.list().size() > 0) ? query.list().get(0) : null);
	}

}
